package co.fitstart.admin.web.controller.market;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Page;
import com.demo2do.core.web.utils.WebUtils;

import co.fitstart.admin.service.MarketCoursewareService;
import co.fitstart.admin.service.UserService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.entity.market.CreditRecord;
import co.fitstart.entity.market.MarketCourseRound;
import co.fitstart.entity.user.User;

@Controller
@RequestMapping("/market-users")
@MenuSetting("menu-market")
public class MarketUserController {
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private MarketCoursewareService marketCoursewareService; 
    
    /**
     * 
     * @param page
     * @param request
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-market-user")
    public ModelAndView list(Page page, HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("market/market-user/market-user-list");
        
        List<User> users = userService.listUserByMarketCourseRound(page);
        modelAndView.addObject("users", users);
        
        modelAndView.addObject("page", page);
        
        modelAndView.addObject("refer", "list");
        modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page"));
        
        return modelAndView;
    }
    
    /**
     * 
     * @param userId
     * @return
     */
    @RequestMapping(value = "/{userId}", method = RequestMethod.GET)
    @MenuSetting("submenu-market-user")
    public ModelAndView view(@PathVariable("userId") Long userId) {
        ModelAndView modelAndView = new ModelAndView("market/market-user/market-user-view");
        
        User user = userService.load(userId);
        modelAndView.addObject("user", user);
        
        MarketCourseRound marketCourseRound = marketCoursewareService.loadMarketCourseRound(user);
        modelAndView.addObject("courseRound", marketCourseRound);
        
        List<CreditRecord> creditRecords = marketCoursewareService.listCreditRecord(user);
        modelAndView.addObject("creditRecords", creditRecords);
        
        return modelAndView;
    }

}
